import { getConfig, statisticRealTime } from '@/api/app'
import { getInStationCount } from '@/api/demand/lists';
import { getToken } from '@/utils/auth'
import { defineStore } from 'pinia'
interface AppSate {
    config: Record<string, any>
    isMobile: boolean
    isCollapsed: boolean
    isRouteShow: boolean,
    msgCount: number | string,
    realData: any
}

const useAppStore = defineStore({
    id: 'app',
    state: (): AppSate => {
        return {
            config: {},
            isMobile: true,
            isCollapsed: false,
            isRouteShow: true,
            msgCount: 0,
            realData: {}
        }
    },
    actions: {
        getImageUrl(url: string) {
            return url.indexOf('http') ? `${this.config.oss_domain}${url}` : url
        },
        getConfig() {
            return new Promise((resolve, reject) => {
                getConfig()
                    .then((data) => {
                        this.config = data
                        resolve(data)
                    })
                    .catch((err) => {
                        reject(err)
                    })
            })
        },
        setMobile(value: boolean) {
            this.isMobile = value
        },
        toggleCollapsed(toggle?: boolean) {
            this.isCollapsed = toggle ?? !this.isCollapsed
        },
        refreshView() {
            this.isRouteShow = false
            nextTick(() => {
                this.isRouteShow = true
            })
        },
        async getCount() {
            if (getToken()) {
                try{
                    const res = await getInStationCount({})
                    this.msgCount = res.count;
                }catch(error){

                }
               
            }
            
        },
        async getReal() {
            if (getToken()) {
                try{
                    const res = await statisticRealTime({})
                    console.log(res)
                    this.realData = res;
                }catch(error){
                    console.log(error)
                }
               
            }
            
        },
    }
})

export default useAppStore
